package com.example.lenovo.mywechat.homepage;

import android.app.Activity;
import android.app.FragmentManager;
import android.view.View;

import com.example.lenovo.mywechat.R;
import com.example.lenovo.mywechat.homepage.find.FindFragment;
import com.example.lenovo.mywechat.homepage.mail_list.MailListFragment;
import com.example.lenovo.mywechat.homepage.me.MeFragment;
import com.example.lenovo.mywechat.homepage.wechat.WechatFragment;

public class Homepage implements View.OnClickListener {
    private FragmentManager fragmentManager;//管理“微信”、“通讯录”、“发现”、“我”四个页面。

    private MouldPage currentMouldPage;//相当于指针，指向当前被选中的页面。

    private MouldPage wechatMouldPage;//“微信”页面。
    private MouldPage mailListMouldPage;//“通讯录”页面。
    private MouldPage findMouldPage;//“发现”页面。
    private MouldPage meMouldPage;//“我”页面

    public Homepage(Activity mActivity) {
        //初始化成员变量
        this.fragmentManager = mActivity.getFragmentManager();

        this.wechatMouldPage = new MouldPage(mActivity, fragmentManager, new WechatFragment(), R.id.ll_wechat, R.id.ib_wechat, R.id.tv_wechat);
        this.mailListMouldPage = new MouldPage(mActivity, fragmentManager, new MailListFragment(), R.id.ll_mail_list, R.id.ib_mail_list, R.id.tv_mail_list);
        this.findMouldPage = new MouldPage(mActivity, fragmentManager, new FindFragment(), R.id.ll_find, R.id.ib_find, R.id.tv_find);
        this.meMouldPage = new MouldPage(mActivity, fragmentManager, new MeFragment(), R.id.ll_me, R.id.ib_me, R.id.tv_me);

        this.init();
        this.addOnClickListener();
    }
    private void init() {
        //为成员变量中的成员变量是指参数。
        this.wechatMouldPage.setNormalImageResource(R.drawable.wechat_normal);
        this.wechatMouldPage.setPressedImageResource(R.drawable.wechat_pressed);

        this.mailListMouldPage.setNormalImageResource(R.drawable.mail_list_normal);
        this.mailListMouldPage.setPressedImageResource(R.drawable.mail_list_pressed);

        this.findMouldPage.setNormalImageResource(R.drawable.find_normal);
        this.findMouldPage.setPressedImageResource(R.drawable.find_pressed);

        this.meMouldPage.setNormalImageResource(R.drawable.me_normal);
        this.meMouldPage.setPressedImageResource(R.drawable.me_pressed);

        //初始化显示哪个页面
        currentMouldPage = wechatMouldPage;
        this.wechatMouldPage.showPage();
    }
    private void addOnClickListener () {
        //为主页下方四个按钮添加监听事件。
        this.wechatMouldPage.addOnClickListener(this);
        this.mailListMouldPage.addOnClickListener(this);
        this.findMouldPage.addOnClickListener(this);
        this.meMouldPage.addOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        currentMouldPage.hidePage();

        switch (v.getId()) {
            case R.id.ll_wechat:
                this.wechatMouldPage.showPage();
                currentMouldPage = wechatMouldPage;
                break;
            case R.id.ll_mail_list:
                this.mailListMouldPage.showPage();
                currentMouldPage = mailListMouldPage;
                break;
            case R.id.ll_find:
                this.findMouldPage.showPage();
                currentMouldPage = findMouldPage;
                break;
            case R.id.ll_me:
                this.meMouldPage.showPage();
                currentMouldPage = meMouldPage;
                break;
            default:
                break;
        }
    }
}